package com.kanche.bigbaby;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.kanche.bigbaby.bean.ResponseMcSigninBean;
import com.kanche.bigbaby.support.base.BaseActivity;
import com.kanche.bigbaby.support.ddlv.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Chenshao_Young on 15/5/20.
 */
public class LoginActivity extends BaseActivity{

    Button btn_login;
    EditText et_login_id,et_password;
    ProgressBar pb;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    void showPb(boolean is){
        if(is){
            pb.setVisibility(View.VISIBLE);
            btn_login.setVisibility(View.INVISIBLE);
        } else {
            pb.setVisibility(View.GONE);
            btn_login.setVisibility(View.VISIBLE);

        }
    }
    @Override
    protected void initViewAndListenerAndData() {
        btn_login = justFindIt(R.id.btn_login);
        et_login_id = justFindIt(R.id.et_login_id);
        et_password = justFindIt(R.id.et_password);
        pb = justFindIt(R.id.pb);

        et_login_id.setText("55555");
        et_password.setText("55555");

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_login_id.getText().toString())) {
                    et_login_id.setError("not empty");
                    return;
                }
                if (TextUtils.isEmpty(et_password.getText().toString())) {
                    et_password.setError("not empty");
                    return;
                }
                Map<String, String> map = new HashMap<String, String>();
                map.put("mobile", et_login_id.getText().toString().trim());
                map.put("password", et_password.getText().toString().trim());
                showPb(true);
                getBBService().merchantSignin(map, new Callback<ResponseMcSigninBean>() {
                    @Override
                    public void success(ResponseMcSigninBean responseMcSigninBean, Response response) {
                        showPb(false);

                        BigBabyApplication.saveAccount(getApplicationContext(),responseMcSigninBean);
                        Intent intent = new Intent(getApplicationContext(), MainToolbarActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        showPb(false);
                        ToastUtils.show(getApplication(), "login failed" + error.getKind().toString());

                    }
                });
            }
        });
        btn_login.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainToolbarActivity.class);
                startActivity(intent);
                finish();

                return true;
            }
        });

    }

}
